package com.team06.util;

import com.team06.pojo.Depe;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import java.io.File;

public class DepeXmlParserUtil {
    public static Depe parseXml(File xmlFile) {
        Depe depe = new Depe();
        try {
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(xmlFile);
            Element root = document.getRootElement();

            // 解析 META 节点
            Element meta = root.getChild("META");
            depe.setMetaSnDR(meta.getChildText("SNDR"));
            depe.setMetaRCVR(meta.getChildText("RCVR"));
            depe.setMetaSEQN(Integer.parseInt(meta.getChildText("SEQN")));
            depe.setMetaDDTM(meta.getChildText("DDTM"));
            depe.setMetaTYPE(meta.getChildText("TYPE"));
            depe.setMetaSTYP(meta.getChildText("STYP"));

            // 解析 DFLT 节点
            Element dflt = root.getChild("DFLT");
            depe.setDfltFLID(dflt.getChildText("FLID"));
            depe.setDfltFATT(dflt.getChildText("FATT"));
            depe.setDfltFFID(dflt.getChildText("FFID"));
            depe.setDfltFIDE(dflt.getChildText("FIDE"));
            depe.setDfltSTAT(dflt.getChildText("STAT"));
            depe.setDfltISTA(dflt.getChildText("ISTA"));
            depe.setDfltMsta(dflt.getChildText("MSTA"));
            depe.setDfltMist(dflt.getChildText("MIST"));
            depe.setDfltFrtt(dflt.getChildText("FRTT"));

            return depe;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解析 Depe XML 失败: " + xmlFile.getAbsolutePath(), e);
        }
    }
}